package com.vim.modules.web.service;

import com.vim.common.ShopConstants;
import com.vim.common.utils.IdGen;
import com.vim.common.base.CrudServiceImpl;
import com.vim.modules.web.model.ShopArea;
import com.vim.modules.web.dao.ShopAreaMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

@Service
public class ShopAreaService extends CrudServiceImpl<ShopArea> {

    @Resource
    private ShopAreaMapper shopAreaMapper;

    @Autowired
    public ShopAreaService(ShopAreaMapper mapper) {
        super(mapper);
    }

    @Override
    public int save(ShopArea entity) {
        entity.setAreaId(IdGen.uuid());
        return super.save(entity);
    }

    /**
     * 地址拼接成字符串形式(如: 湖北省武汉市江夏区)
     * @param areaId 区ID
     */
    public String areaInfo(String areaId){
        if(StringUtils.isBlank(areaId)){
            return "";
        }

        ShopArea area = find(areaId);
        if(null == area){
            return "";
        }

        if(area.getAreaDeep() != ShopConstants.AREA_DEEP.AREA_TYPE){
            return "";
        }

        ShopArea cityArea = find(area.getAreaParentId());
        if(null == cityArea){
            return "";
        }
        ShopArea provinceArea = find(cityArea.getAreaParentId());
        if(null == provinceArea){
            return "";
        }
        return provinceArea.getAreaName()+cityArea.getAreaName()+area.getAreaName();
    }
}

